//app.js
import util from './utils/util.js';
import $ from './utils/ajax.js';
App({
    globalData: {},
    wxApp: {
        // url: 'https://apis.52xfj.com/',
        url: 'https://apitest.52xfj.com/',
        // url: 'http://192.168.1.15:8080/',
        tel: '4000552199',
        imgAddress: '',
    },
    onStrart() {
        wx.showLoading({
            mask: true,
            title: '加载中'
        })
    },
    onShop() {
        wx.hideLoading();
    },
    quit() {
        wx.clearStorage();
        wx.reLaunch({
            url: '/pages/login/login'
        })
    },
    agentStatus() {
        $.p(this.wxApp.url + 'agentnew/jump', {
            tel: wx.getStorageSync('tel'),
            agent: wx.getStorageSync('agent')
        }, () => { }, (res) => {
            this.agentStatusSwitch(res.data);
        }, (res) => {
            util.showToast(res.data)
        })
    },
    agentStatusSwitch(data) {
        /**
         *  agentType(role)   运营商类型  1:省运营，2：市运营；3特约商户; 9:个人运营
         *  status      账户状态 1未开户,2激活,3冻结,4注销
         *  auditStatus 审核状态 1-未审核 2-审核通过 3-审核驳回
         *  applyStatus 申请步骤 1-选择身份 2-提交资料 3-提交企业资质照片
         */
        switch (data.role) {
            case 0:
                wx.reLaunch({
                    url: '/pages/login/agent/agent'
                })
                break;
            case 1:
            case 2:
            case 3:
                if (data.status === 1 || data.status === 0) {
                    if (data.applyStatus === 3) {
                        //审核
                        wx.reLaunch({
                            url: '/pages/auditing/auditing?item=' + JSON.stringify(data)
                        });
                    } else if (data.applyStatus === 2) {
                        wx.reLaunch({
                            url: '/pages/examine/photo/photo?item=' + JSON.stringify(data)
                        })
                    } else if (data.applyStatus === 1) {
                        wx.reLaunch({
                            url: '/pages/examine/collective/collective?item=' + JSON.stringify(data)
                        })
                    }
                } else if (data.status === 2) {
                    //首页
                    wx.switchTab({
                        url: '/pages/index/index'
                    })
                }
                break;
            case 9: 
                wx.switchTab({
                    url: '/pages/index/index'
                })
                break;

        }
    },
    login(sucess, fail) {
        const that = this;
        if (!this.globalData.user) {
            wx.login({
                success: function (res) {
                    const code = res.code;
                    console.log(code)
                    wx.getUserInfo({
                        success: function (res) {
                            $.p(that.wxApp.url + '/wechat/mini/login', {
                                code: code
                            }, that.onStrart, (res) => {
                                wx.setStorageSync('openId', res.data.openid);
                                sucess(res);
                                that.onShop();
                            }, (res) => {
                                that.onShop();
                            })
                        },
                        fail: function (res) {
                            fail();
                        }
                    })
                },
                fail: function (res) {
                    fail();
                }
            })
        } else {
            sucess(user);
        }
    }
})